package com.edgclub.edg.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.edgclub.edg.R;
import com.edgclub.edg.activity.ImageViewPagerActivity;
import com.edgclub.edg.model.PostsAlbum;
import com.edgclub.edg.utils.GlideUtil.GlideUtils;
import com.edgclub.edg.utils.util.DensityUtils;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

public class OtherUserHonerAdapter extends BaseAdapter{
    private Context mContext;
    private List<PostsAlbum>list;
    GlideUtils glideUtils=new GlideUtils();
    boolean res;
    private int itemW;
    public OtherUserHonerAdapter(Context context,boolean res) {
        super();
        itemW = (DensityUtils.getScreenW((Activity) context)-30) / 3;
        mContext = context;
        this.res=res;
    }

    public void setList(List<PostsAlbum> list) {
        this.list = list;
        if(null==this.list)this.list=new ArrayList<>();
    }

    @Override
    public int getCount() {
        if(list.size()>3){
            return 3;
        }else{
            return list.size();
        }
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (null == convertView) {
            convertView = LayoutInflater.from(mContext).inflate(
                    R.layout.item_user_image, null);
            holder = new ViewHolder();
            holder.icon_iv = (ImageView) convertView.findViewById(R.id.user_image);

            convertView.setTag(holder);
        }else {
            holder= (ViewHolder) convertView.getTag();
        }
        RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) holder.icon_iv.getLayoutParams();
        lp.width = itemW;
        lp.height = itemW;
        holder.icon_iv.setLayoutParams(lp);
        if(res) {
            glideUtils.Glidde_loadImage(mContext, list.get(position).getThumbnail() + "", holder.icon_iv);
            holder.icon_iv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent = new Intent(mContext,
                            ImageViewPagerActivity.class);
                    intent.putExtra("urls", (Serializable) list);
                    intent.putExtra("position", position);
                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    mContext.startActivity(intent);
                    ((Activity) mContext).overridePendingTransition(R.anim.head_in, R.anim.head_out);
                }
            });
        }else {
            glideUtils.Glidde_loadImage1(mContext, list.get(position).getImgUrl() + "", holder.icon_iv);

        }

        return convertView;
    }
    class ViewHolder {
        private ImageView icon_iv;
    }


//    viewHolder.image.setOnClickListener(new View.OnClickListener() {
//        @Override
//        public void onClick(View view) {
//            Intent intent = new Intent(mContext,
//                    ImageViewPagerActivity.class);
//            intent.putExtra("urls", postsAlbums);
//            intent.putExtra("position", position);
//            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//            mContext.startActivity(intent);
//            ((Activity) mContext).overridePendingTransition(R.anim.head_in, R.anim.head_out);
//        }
//    });
}
